<?php
namespace app\api\controller;
use app\api\model\News as NewsModel;

class News extends Controller
{
public function index(){
    $model=new NewsModel();
    $news=$model->select();
    //dump($news);
  return $this->renderSuccess($news,'获取新闻成功');

}
    public function search($id){
        $model=new NewsModel();
        $user=$model->where('id','=',$id)->find();

        return $this->renderSuccess($user,'获取成功');
    }
    public function del($id)
    {
        if (NewsModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    public function edit($id){
        $model=new NewsModel();
        if ($model->update($this->request->param())->where('id','=',$id)) {

            return $this->renderSuccess([],'修改成功');
        } else {
            return $this->renderError('修改失败');
        }
    }

    public function add()
    {
        $model = new NewsModel();
        if ($model->save($this->request->param())) {
            $new_news = $model->where(['id' => $model->getLastInsID()])->find();
            return $this->renderSuccess($new_news, '添加成功');
        } else {
            return $this->renderError('添加失败');
        }
    }
}
